﻿using System.Windows.Media;
using Wulong.Phone.Qiushi.ViewModels;
using Wulong.Phone.Utils;

namespace Wulong.Phone.Qiushi
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Controls;
    using Microsoft.Phone.Controls;
    using Caliburn.Micro;

    public class AppBootstrapper : PhoneBootstrapper
    {
        static AppBootstrapper()
        {
            //for caliburn debug
            LogManager.GetLog = type => new DebugLogger(type);
        }
        private PhoneContainer _container;

        protected override void Configure()
        {
            _container = new PhoneContainer(RootFrame);

            _container.RegisterPhoneServices();
            _container.Singleton<MainViewModel>();
            _container.PerRequest<QiushiListViewModel>();
            _container.PerRequest<CommentsViewModel>();
            _container.PerRequest<NewPostViewModel>();
            _container.PerRequest<PicViewModel>();
            _container.PerRequest<AboutUsViewModel>();
            _container.PerRequest<SplashViewModel>();
            
            AddCustomConventions();
        }

        protected override PhoneApplicationFrame CreatePhoneApplicationFrame()
        {
            return new TransitionFrame
            {
                //doing so due the toolkit transition affecting performance
                //See Matt Lacey's blog for detail
                Background = new SolidColorBrush(Colors.Transparent)
            };
        }

        protected override object GetInstance(Type service, string key)
        {
            return _container.GetInstance(service, key);
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return _container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            _container.BuildUp(instance);
        }

        private static void AddCustomConventions()
        {
            ConventionManager.AddElementConvention<Pivot>(Pivot.ItemsSourceProperty, "SelectedItem", "SelectionChanged")
                .ApplyBinding =
                (viewModelType, path, property, element, convention) =>
                    {
                        if (ConventionManager
                            .GetElementConvention(typeof (ItemsControl))
                            .ApplyBinding(viewModelType, path, property, element, convention))
                        {
                            ConventionManager
                                .ConfigureSelectedItem(element, Pivot.SelectedItemProperty, viewModelType, path);
                            ConventionManager
                                .ApplyHeaderTemplate(element, Pivot.HeaderTemplateProperty, viewModelType);
                            return true;
                        }

                        return false;
                    };

            ConventionManager.AddElementConvention<Panorama>(Panorama.ItemsSourceProperty, "SelectedItem",
                                                             "SelectionChanged").ApplyBinding =
                (viewModelType, path, property, element, convention) =>
                    {
                        if (ConventionManager
                            .GetElementConvention(typeof (ItemsControl))
                            .ApplyBinding(viewModelType, path, property, element, convention))
                        {
                            ConventionManager
                                .ConfigureSelectedItem(element, Panorama.SelectedItemProperty, viewModelType, path);
                            ConventionManager
                                .ApplyHeaderTemplate(element, Panorama.HeaderTemplateProperty, viewModelType);
                            return true;
                        }

                        return false;
                    };

            ConventionManager.AddElementConvention<ListPicker>(ListPicker.ItemsSourceProperty, "SelectedItem",
                                                               "SelectionChanged")
                .ApplyBinding = (viewModelType, path, property, element, convention) =>
                                    {
                                        if (ConventionManager.GetElementConvention(typeof (ItemsControl))
                                            .ApplyBinding(viewModelType, path, property, element, convention))
                                        {
                                            ConventionManager.ConfigureSelectedItem(element,
                                                                                    ListPicker.SelectedItemProperty,
                                                                                    viewModelType, path);
                                            return true;
                                        }
                                        return false;
                                    };
            ConventionManager.AddElementConvention<ContextMenu>(ItemsControl.ItemsSourceProperty, "DataContext", "Click");
            ConventionManager.AddElementConvention<MenuItem>(ItemsControl.ItemsSourceProperty, "DataContext", "Click");
        }
    }
}